package com.thenie.dao.impl;

import com.thenie.dao.BookDao;
import com.thenie.pojo.Book;

import java.util.List;

public class BookDaoImpl extends BaseDao implements BookDao {
    @Override
    public int addBook(Book book) {
        String sql="insert into t_book(`name`,`author`,`price`,`sales`,`stock`,`img_path`)values(?,?,?,?,?,?)";
        return update(sql, book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath());
    }

    @Override
    public int deleteBookById(Integer id) {
        String sql="delete from t_book where id=?";
        return update(sql, id);
    }

    @Override
    public int updateBook(Book book) {
        String sql="update t_book set `name`=?,`author`=?,`price`=?,`sales`=?,`stock`=?,`img_path`=? where id = ?";
        return update(sql, book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath(),book.getId());
    }

    @Override
    public Book queryBook(Integer id) {
        String sql="select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgPath from t_book where id=?";
        return queryForOne(Book.class, sql, id);
    }

    @Override
    public List<Book> queryBooks() {
        String sql="select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgPath from t_book";
        return queryForList(Book.class, sql);
    }


    @Override
    public Integer queryPageTotalCount() {
        String sql="SELECT COUNT(*)pageTotalCount FROM t_book";
        Number count=(Number)queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<Book> queryPageItems(int begin,int pageSize) {
        String sql="select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgPath from t_book limit ?,?";
        return queryForList(Book.class, sql, begin,pageSize);
    }

    @Override
    public Integer queryPageTotalCountOnPrice(int min, int max) {
        String sql="SELECT COUNT(*) FROM t_book WHERE `price`>? AND `price`<?";
        Number count=(Number)queryForSingleValue(sql, min,max);
        return count.intValue();
    }

    @Override
    public List<Book> queryPageItemsOnPrice(int begin, int pageSize, int min, int max) {
        String sql="SELECT `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path`AS imgPath FROM t_book WHERE `price`>? AND `price`<? ORDER BY `price` LIMIT ?,?";
        List<Book> books = queryForList(Book.class, sql, min, max, begin, pageSize);
        return books;
    }
}
